<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose" :close-on-click-modal="false">
      <el-form :model="formClient" ref="formClient" :rules="rules">
        <el-row>
          <el-col :span="24">
            <el-form-item label="单位类型" :label-width="formLabelWidth" prop="customerType">
              <el-checkbox-group v-model="formClient.customerType">
                <el-checkbox label="0">供应商</el-checkbox>
                <el-checkbox label="1">客户</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位编号" prop="customerCode" :label-width="formLabelWidth">
              <el-input v-model="formClient.customerCode" placeholder="请输入单位编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位名称" prop="customerName" :label-width="formLabelWidth">
              <el-input v-model="formClient.customerName" placeholder="请输入单位名称"></el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="助记码" prop="mnemonicCode" :label-width="formLabelWidth">-->
<!--              <el-input v-model="formClient.mnemonicCode" placeholder="请输入助记码"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="地址" prop="address" :label-width="formLabelWidth">
              <el-input v-model="formClient.address" placeholder="请输入地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactPerson" :label-width="formLabelWidth">
              <el-input v-model="formClient.contactPerson" placeholder="请输入联系人"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone" :label-width="formLabelWidth">
              <el-input v-model="formClient.contactPhone" placeholder="请输入联系电话"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
              <el-input v-model="formClient.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税号" prop="taxNo" :label-width="formLabelWidth">
              <el-input v-model="formClient.taxNo" placeholder="请输入税号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营范围" prop="businessScope" :label-width="formLabelWidth">
              <el-input v-model="formClient.businessScope" placeholder="请输入经营范围"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="简称" prop="shortName" :label-width="formLabelWidth">
              <el-input v-model="formClient.shortName" placeholder="请输入简称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark" :label-width="formLabelWidth">
              <el-input v-model="formClient.remark" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户银行" prop="bankName" :label-width="formLabelWidth">
              <el-input v-model="formClient.bankName" placeholder="请输入开户银行"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankAccount" :label-width="formLabelWidth">
              <el-input v-model="formClient.bankAccount" placeholder="请输入银行账号"></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="资质信息" :label-width="formLabelWidth">
              <ImageUpload v-model="formClient.picPath"></ImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addSupplier,//新增供应商
  editSupplier,//修改供应商
} from '@/api/basic/PersonnelInformation.js'
import ImageUpload from '@/components/ImageUpload/index'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formClient: {
        customerType: [],
      },
      title: '新增往来单位',
      formLabelWidth: '140px',
      rules: {
        customerCode: [
          {required: true, message: '请输入单位编号', trigger: 'blur'},
        ],
        customerName: [
          {required: true, message: '请输入单位名称', trigger: 'blur'},
        ],
        // mnemonicCode: [
        //   {required: true, message: '请输入助记码', trigger: 'blur'},
        // ],
        customerType:[
          {required: true, message: '请选择单位类型', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    //获取当前行信息
    openDialog(type, val) {
      this.title = type === 'edit' ? '编辑往来单位' : '新增往来单位';
      if (type === 'edit' && val) {
        this.formClient = {
          ...val,
        };
        this.formClient.customerType = val.typeList;
      } else {
        this.formClient = {
          customerType: [],
        };
      }
    },
    //提交表单
    submit() {
      this.$refs.formClient.validate((valid) => {
        if (valid) {
          const apiMethod = this.title === '新增往来单位' ? addSupplier : editSupplier;
          this.formClient.customerType = this.formClient.customerType.join(',')
          const payload = {
            ...this.formClient,
          };
          apiMethod(payload)
            .then(res => {
              this.$emit('addSuccess');
              this.$message.success(res.data.msg);
              this.handleClose();
            })
            .catch(err => {
              this.$message.error(err?.msg || '操作失败');
            });
        }
      });
    },
    // 提取重置表单的逻辑
    handleClose() {
      // 通过事件通知父组件更新visible值，触发update:visible事件
      this.$emit('update:visible', false);
      this.$nextTick(() => {
        this.$refs.formClient.resetFields();
      })
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep.el-dialog__body {
  height: 650px;
  overflow-y: auto;
}
</style>
